/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-20     great_master       the first version
 */
#include "board.h"
#include "relay.h"

#define RELAY_CTRL_PIN_NAME "PA.0"
rt_base_t m_relay_pin;

static bool is_realy_initialized = false;

void relay_on(void)
{
    if(is_realy_initialized)
    {
        rt_pin_write(m_relay_pin, PIN_HIGH);
    }
}
FINSH_FUNCTION_EXPORT(relay_on, relay on);

void relay_off(void)
{
    if(is_realy_initialized)
    {
        rt_pin_write(m_relay_pin, PIN_LOW);
    }
}
FINSH_FUNCTION_EXPORT(relay_off, relay off);

int relay_init(void)
{
    rt_kprintf("%s\r\n", __func__);
    m_relay_pin = rt_pin_get(RELAY_CTRL_PIN_NAME);

    if(m_relay_pin == -RT_EINVAL)
    {
        rt_kprintf("relay pin get failure\r\n");
        return m_relay_pin;
    }
    is_realy_initialized = true;
    rt_pin_mode(m_relay_pin, PIN_MODE_OUTPUT);
    rt_pin_write(m_relay_pin, PIN_LOW);

    return RT_EOK;
}

